//
//  LocalFileManager.m
//  Toyohu
//
//  Created by GuoPeng on 16/4/21.
//  Copyright © 2016年 celia. All rights reserved.
//

#import "LocalFileManager.h"



@implementation LocalFileManager

+(void)saveImage:(UIImage *)image forKey:(NSString *)key
{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *dir = [NSString stringWithFormat:@"%@/User",path];
    [self createPathIfNeeded:dir];
    
    NSString *userInfoPath = [NSString stringWithFormat:@"%@/User/%@",path,key];
    [UIImagePNGRepresentation(image) writeToFile:userInfoPath atomically:YES];
}

+ (NSString *)getImageFile:(NSString *)key
{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *iconPath = [NSString stringWithFormat:@"%@/User/%@",path,key];
    return iconPath;
}

+ (UIImage *)getImageForKey:(NSString *)key
{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *iconPath = [NSString stringWithFormat:@"%@/User/%@",path,key];
    if (![[NSFileManager defaultManager] fileExistsAtPath:iconPath]) {
        return nil;
    }
    
    return [UIImage imageWithContentsOfFile:iconPath];
}

#pragma mark ---private方法

+ (BOOL)createPathIfNeeded:(NSString *)path
{
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        return YES;
    }
    
    BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    return success;
    
}


@end
